const path = require('path');
const webpack = require('webpack');
const VueLoaderPlugin  = require('vue-loader/lib/plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin')

function compareTo10(num) {
    if(num < 10){
        num = "0" + num
    }
    return num;
}
function formatDate(now) {
    let year=now.getFullYear();
    let month=now.getMonth()+1;
    let date=now.getDate();
    let hour=now.getHours();
    let minute=now.getMinutes();
    let second=now.getSeconds();
    return year + compareTo10(month) + compareTo10(date) + compareTo10(hour) + compareTo10(minute) + compareTo10(second);
}
const Version = formatDate(new Date());

let config = {
    entry: path.join(__dirname, 'src/main.js'), // 项目总入口js文件
    output: {
        publicPath: '/',
        path: path.join(__dirname, 'dist'), // 所有的文件都输出到dist/目录下
        filename: 'bundle.js',
        chunkFilename: './static/js/[name].[hash:8].'+Version+'.js',
        assetModuleFilename: 'static/images/[name].[hash:8].'+Version+'[ext]'
    },
    module: {
        rules: [
            {
                // 使用vue-loader解析.vue文件
                test: /\.vue$/,
                loader: 'vue-loader',
                options: {
                    transformToRequire: {
                        video: ['src', 'poster'],
                        source: 'src',
                        img: 'src',
                        image: 'xlink:href'
                    }
                }
            },
            {
                test: /\.(png|jpg|jpeg|gif|ico)$/,
                type: 'asset/resource',
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader",
                options: {
                    presets: ['@babel/preset-env']
                }
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            title: 'vue-app-base',
            template:'./public/index.html'
        }),
        new webpack.DefinePlugin({
            BASE_URL: JSON.stringify('/static/images/')
        }),
        new CopyWebpackPlugin({
            patterns:[
                {
                    from: './*/*.ico', 
                    to : 'static/images/[name][ext]'
                }
            ]
        })
    ]
};
module.exports = config;